// pages/userInfoCenter/index.js
const App = getApp();
const {Dialog, Toast} = require("../../../../../../config/modal")
const utils = require("../../../../../../utils/util")
Page({
    /**
     * 页面的初始数据
     */
    data: {
        theamTextColor: App.globalData.theamTextColor,
        popupShow: false,
        // 轮播
        swiperImgs: [{
            url: "/static/img/integral-side.jpeg",
            title: ''
        }, {
            url: "/static/img/integral-side2.jpeg",
            title: ''
        }],
        goods: {
            goodsNo: '',
            item: {},
            numErrorMsg: "",
            buyForm: {
                addressId: "",
                goodsNo: "",
                num: 0,
            }
        },
        address: null,
        detailView: {
            marginBottom: 160
        }
    },

    URL: {
        getDetail: "/integral/goods/detail/?",
        getDefaultAddr: "/address/getDefaultAddr",
        toBuy: '/integral/goods/toBuy',
    },
    page: {
        address: "/pages/tabbar/user/integral/exchange/address/index",
        GoodsDetail: "/pages/tabbar/user/integral/exchange/goods-detail/index",
        exchange: "/pages/tabbar/user/integral/exchange/index",
        records: "/pages/tabbar/user/integral/exchange/records/index",
    },


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        wx.hideShareMenu();
        //'2091ad637adf46caa143e91c9c2192f8',//
        utils.saveFromPage(App, getCurrentPages());
        // 地址页面进来带来的参数
        if (!utils.isInvalid(options.address)) {
            let item = JSON.parse(options.address);
            this.setData({
                'address': item,
                'goods.buyForm.addressId': item.id,
            })
        }

        //'2091ad637adf46caa143e91c9c2192f8',//
        this.setData({
            'goods.goodsNo': options.goodsNo,
            'goods.buyForm.goodsNo': options.goodsNo
        })
    },

    // 获取积分商品
    initGoodsDetail() {
        App.$get(true, this.URL.getDetail.replace("?", this.data.goods.goodsNo), {}, res => {
            if (res.R) {
                let imgs = res.data.swiper;
                let swipers = []
                imgs.forEach(img => {
                    swipers.push({url: img})
                })
                this.setData({
                    'goods.item': res.data,
                    'goods.buyForm.num': res.data.stock == 0 ? 0 : 1,
                    "swiperImgs": swipers
                })
            }
        })
    },
    // 获取用户默认地址
    getDefaultAddr() {
        if (App.$isAuth && utils.isInvalid(this.data.address)) {
            App.$get(true, this.URL.getDefaultAddr, {}, res => {
                if (res.R) {
                    this.setData({
                        'address': res.data,
                        'goods.buyForm.addressId': res.data.id,
                    })
                }
            })
        }
    },

    onShow() {
        this.initGoodsDetail();
        this.getDefaultAddr();
    },

    // 地址选择
    toAddressPage() {
        App.$navigateTo(this.page.address + "?flag=back&goodsNo=" + this.data.goods.goodsNo, true);
    },


    // 关闭弹出层
    onClosePopup() {
        this.setData({
            "popupShow": false,
        })
    },
    // 立即兑换按钮 显示弹出层
    onClickButton() {
        this.setData({
            "popupShow": true,
        })
    },

    // 计数器
    stockChange(e) {
        if (e.detail === 0) {
            this.setData({
                'goods.numErrorMsg': '*数量必须大于:0',
                'goods.buyForm.num': e.detail,
            })
            return;
        }
        this.setData({
            'goods.buyForm.num': e.detail,
            'goods.numErrorMsg': '',
        })
    },

    // 提交按钮
    onSubmitBuy() {
        let item = this.data.goods.item;
        if (item.stock === 0) {
            Dialog.alert({
                message: '商品库存不足',
                width: "250px",
                confirmButtonColor: this.data.theamTextColor,
                confirmButtonText: "关闭",
            })
            return;
        }
        if (item.status === "下架") {
            Dialog.alert({
                message: '当前商品已下架',
                width: "250px",
                confirmButtonColor: this.data.theamTextColor,
                confirmButtonText: "关闭",
            })
            return;
        }

        if (this.data.goods.buyForm.num === 0) {
            return;
        }

        this.setData({
            "popupShow": false,
        })

        App.$post(true, this.URL.toBuy, this.data.goods.buyForm, res => {
            if (res.R) {
                // 刷新库存
                this.initGoodsDetail();
                Dialog.alert({
                    message: '兑换成功',
                    width: "250px",
                    confirmButtonColor: this.data.theamTextColor,
                    confirmButtonText: "关闭",
                }).then(()=>{
                    //App.$navigateTo(this.page.records, true);
                })
            }
        })
    },
    // 分享按钮
    onClickShareIcon(){

    },
    // 返回
    goBack() {
        App.$redirectTo(this.page.exchange, false);
    },
    showImg(e) {
        let imgUrl = e.currentTarget.dataset.img;
        let swiperImgs = e.currentTarget.dataset.swiperImgs;
        wx.previewImage({
            current: imgUrl,
            urls: swiperImgs
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },


    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
})
